package ubadb.core.components.bufferManager.bufferPool.replacementStrategies.MRU;

import java.util.Collection;
import java.util.Date;

import ubadb.core.common.Page;
import ubadb.core.components.bufferManager.bufferPool.BufferFrame;
import ubadb.core.components.bufferManager.bufferPool.replacementStrategies.LRU.LRUBufferFrame;
import ubadb.core.components.bufferManager.bufferPool.replacementStrategies.PageReplacementStrategy;
import ubadb.core.exceptions.PageReplacementStrategyException;

public class MRU implements PageReplacementStrategy {
	public BufferFrame findVictim(Collection<BufferFrame> bufferFrames) throws PageReplacementStrategyException{
		LRUBufferFrame victim = null;
		Date olderF = null; //horrible pero bue

		for (BufferFrame bufferFrame : bufferFrames) {
			LRUBufferFrame LRUBF = (LRUBufferFrame) bufferFrame;		//mm si la casteo donde esta la fecha?
			// LRUBF = LRU BufferFrame
			if (LRUBF.canBeReplaced() && ((olderF == null) || LRUBF.getPDate().after(olderF))) {
				victim = LRUBF;
				olderF = LRUBF.getPDate();
			}
		}
		
		if (victim == null)
			throw new PageReplacementStrategyException("Failed to find Victim");
		return victim;
	}

	public BufferFrame createNewFrame(Page page) {
		return new LRUBufferFrame(page);
	}
}